package net.zjitc.controller;

import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Role;
import net.zjitc.exception.RoleException;
import net.zjitc.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 毛钰深
 * @Create 2021/12/29 20:43
 */
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/roles")
    public ResponseResult selectRole() {
        return roleService.selectRole();
    }

    @PostMapping("/roles")
    public ResponseResult addRole(@RequestBody Role role) {
        ResponseResult res;
        try {
            if (role == null) {
                throw new RoleException("传入参数为空");
            }
            res = roleService.addRole(role);
        } catch (RoleException e) {
            res = new ResponseResult();
            res.setBadRequest(e.getMessage());
        }
        return res;
    }

    @GetMapping("/roles/{id}")
    public ResponseResult selectById(@PathVariable Integer id) {
        ResponseResult res;
        res = roleService.selectById(id);
        return res;
    }

    @PutMapping("/roles/{id}")
    public ResponseResult updateRole(@PathVariable Integer id, @RequestBody Role role) {
        ResponseResult<Role> res;

        try {
            if (role == null) {
                throw new RoleException("传入参数为空");
            }
            role.setRoleId(id);
            res = roleService.updateRole(role);
        } catch (RoleException e) {
            res = new ResponseResult<>();
            res.setError("传入的修改参数错误");
        }
        return res;
    }

    @DeleteMapping("/roles/{id}")
    public ResponseResult deleteRoleById(@PathVariable Integer id) {
        return roleService.deleteRoleById(id);
    }

    @PostMapping("/roles/{roleId}/rights")
    public ResponseResult rightsAndRole(@PathVariable Integer roleId,@RequestBody Role role) {
        ResponseResult res;
        try {
            if (role.getPsIds() == null) {
                throw new RoleException("传入参数为空");
            }
            //以 `,` 分割的权限 ID 列表（获取所有被选中、叶子节点的key和半选中节点的key, 包括 1，2，3级节点）
            role.setRoleId(roleId);
            //排除其他参数干扰
            role.setRoleName(null);
            role.setRoleDesc(null);
            res=roleService.rightsAndRole(role);
        } catch (RoleException e) {
            res=new ResponseResult();
            res.setError(e.getMessage());
        }

        return res;
    }

    @DeleteMapping("/roles/{roleId}/rights/{rightId}")
    public ResponseResult deleteRoleRights(@PathVariable String roleId,@PathVariable String rightId){
        ResponseResult res;
        try {
            res = roleService.deleteRoleRights(Integer.parseInt(roleId),Integer.parseInt(rightId));
        } catch (RoleException e) {
            res=new ResponseResult();
            res.setError(e.getMessage());
        }
        return res;
    }
}
